package day01.base

import scala.util.matching.Regex

/**
 * @author wsl
 * @version 2022-04-28-13:35
 *          正则表达式:建议使用三个双引号来表示正则表达式，不然就得对正则中的反斜杠进行转义
 *          .匹配任意
 *          +重复多次
 */
object demo4_Regex {
  def main(args: Array[String]): Unit = {

    val pattern: Regex = "Scala".r
    val str = "Scala1 scala2 is scalable3 Language able22 able1"

    println(pattern findFirstIn str) // 匹配字符串第一个
    println(pattern replaceFirstIn(str, "Java")) //替换

    val pattern1 = new Regex("(S|s)cala") //匹配大写 or 小写
    println((pattern1 findAllIn str).mkString(",")) //匹配所有

    val pattern2 = new Regex("abl[ae]\\d+")
    println((pattern2 findAllIn str).mkString(","))

    println(isPhoneNumber("18801234567"))
    println(isPhoneNumber("11111111111"))

  }

  //判断手机号是否合法
  def isPhoneNumber(number: String): Boolean = {
    val regex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))[0-9]{8}$".r
    val length = number.length
    regex.findFirstMatchIn(number.slice(length - 11, length)) != None

  }

  //判断邮箱是否合法
  val email: String = "wsl156230@qq.cpm"
  val emailList = List("38123845@qq.com", "a1da88123f@gmail.com", "zhansan@163.com", "123afadff.com")

  //
  val regex = """.+@.+\..+""".r
  if (regex.findAllMatchIn(email).size != 0) {
    println(s"${email}是一个合法邮箱")
  } else {
    println(s"${email}不合法")
  }


}
